﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OUtl.Setting;

namespace UserLoginTest.common
{
    public partial class frmBase : Form
    {
        public frmBase()
        {
            InitializeComponent();
        }

        private void frmBase_Load(object sender, EventArgs e)
        {

        }

        #region >>Info<<
        protected bool isStart = false;
        protected bool isSend = false; 
        protected string AppName = null;
        protected string UserName { get; set; }
        protected string Password { get; set; }

        protected DateTime StartTime = DateTime.Now;

        protected string SportKey = "";
        #endregion

        #region >>UserClient<<
        protected MSGClient.Client clientUser = null;

        protected void userStart()
        {
            if (isStart)
            {
                isStart = false;
                if (clientUser != null)
                    clientUser.Stop();
                clientUser = null;
            }
            else
            {
                isStart = true;
                StartTime = DateTime.Now;
                clientUser = new MSGClient.Client(AppName, UserName, Password, Settings.ServerIP, MSGGolbalInfo.Settings.DicDefPort["Client"]);
                clientUser.DataReceivedEvent += new EventHandler(clientUser_DataReceivedEvent);
                clientUser.CommandReceivedEvent += new EventHandler(clientUser_CommandReceivedEvent);
                clientUser.ClientStateEvent += new MSGGolbalInfo.ClientStateEventArgs(clientUser_ClientStateEvent);
                clientUser.SettingEvent += new EventHandler(clientUser_SettingEvent);
                clientUser.Start();
                setUserInfo();
            }
            Start(!isStart);
        }

        protected virtual void clientUser_CommandReceivedEvent(object sender, EventArgs e)
        {
        }

        protected virtual void clientUser_DataReceivedEvent(object sender, EventArgs e)
        {
        }

        protected virtual void clientUser_ClientStateEvent(object sender, TCPGolbalInfo.FlagEnum flag)
        {
        }

        protected virtual void clientUser_SettingEvent(object sender, EventArgs e)
        {
        }

        protected virtual void setUserInfo()
        {
        }
        #endregion

        #region >>Start<<
        protected virtual void Start(bool isStop)
        {
        }
        #endregion

        #region >>ServerIP<<

        /// <summary>
        /// 设定服务器IP
        /// </summary>
        protected void setSetverIP()
        {
            frmServerIP frm = new frmServerIP(Settings.ServerIP);
            DialogResult dr = frm.ShowDialog(this);
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                if (frm.ServerIP != Settings.ServerIP)
                {
                    Settings.ServerIP = frm.ServerIP;
                    ServerIPset();
                }
            }
            if (frm != null && !frm.IsDisposed)
                frm.Dispose();
            frm = null;
        }

        /// <summary>
        /// 服务器IP被设定
        /// </summary>
        protected virtual void ServerIPset()
        {
        }
        #endregion

        #region >>Exit<<
        protected void ExitAPP()
        {
            Application.Exit();
            System.Threading.Thread.Sleep(1000);
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }
        #endregion

        #region >>Debug_WriteLine<<
        public static bool isDebug { get; set; }
        protected static void Debug_WriteLine(string msg)
        {
            if (OUtl.OutputBox.isSay && isDebug)
                Console.WriteLine(msg);
        }

        protected static void Debug_WriteLine(string format, params object[] arg)
        {
            if (OUtl.OutputBox.isSay && isDebug)
                Console.WriteLine(format, arg);
        }
        #endregion
    }
}
